<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends MY_Controller {

	public function index()
	{
		$this->load->model('options_model','options');
		$opt = $this->options->get_option($this->menu_option_key);

		$this->load->model('menu_model','menu');
		// Initialize menu
		$this->menu->initialize($opt->option_value)->sort('path','asc');

		$top_menu    = $this->menu->get_menu_level();
		$middle_menu = array();
		$end_menu    = array();
		if ( 0 !== count($top_menu))
		{
			$middle_menu = $this->menu->get_menu_level(hashids_decode($top_menu[0]['id']),1);
		}

		if ( !empty($middle_menu) )
		{
			$end_menu = $this->menu->get_menu_level(hashids_decode($middle_menu[0]['id']),1);
		}

		$data['top_menu']    = $top_menu;
		$data['middle_menu'] = $middle_menu;
		$data['end_menu']    = $end_menu;
		$this->load->view('main/index',$data);
	}


	public function get_menu() {
		$id = hashids_decode($this->input->post('id'));
		if ( empty($id) )
		{
			ajax_return(self::HTTP_STATUS_FAIL,'ID不能为空');
		}

		$this->load->model('options_model','options');
		$opt = $this->options->get_option($this->menu_option_key);

		$this->load->model('menu_model','menu');
		// Initialize menu
		$this->menu->initialize($opt->option_value)->sort('path','asc');
		ajax_return(self::HTTP_STATUS_SUCCESS,'Success',$this->menu->get_menu_level($id));
	}

	public function test()
	{
		$this->load->view('table');
	}
}
